<template>
  <div>
    <!-- 搜索 -->

    <div class="search">

      <label style="color:#666;font-size:12px">角色：</label>
      <el-select size="mini" v-model="roleId" placeholder="请选择角色" clearable style="margin-right:10px">
        <el-option v-for="item in roleListToSearch" :key="item.roleId" :label="item.roleName" :value="item.roleId">
        </el-option>
      </el-select>
      <el-button type="success" size="mini" @click="searchClick">查询</el-button>
      <el-button type="primary" size="mini" @click="addClick">添加</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="tableData1.slice((currentPage - 1) * pageSize, currentPage * pageSize)" style="width:100%" size="mini">
      <el-table-column label="编号" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id==1?'我是id':'' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账号" width="180">
        <template slot-scope="scope">
          {{ scope.row.loginId }}
        </template>
      </el-table-column>
      <el-table-column label="姓名" width="180">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="电话" width="180">
        <template slot-scope="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>
      <el-table-column label="角色" width="180">
        <template slot-scope="scope">
          {{ scope.row.role.roleName }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="flex j-c" style="margin-top:5px">
      <el-pagination class="pagination" align="center" background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[1, 5, 10, 20]" :page-size="pageSize" layout="total, prev, pager, next" :total=" tableData1.length">
      </el-pagination>
    </div>
    <!-- 抽屉 -->
    <el-dialog :append-to-body="true" :title="isAdd ?  '添加账号' : '修改账号'" :visible.sync="drawer">
      <div style="padding:0 10px">
        <el-form size="mini" :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
          <!-- <el-form-item label="标题" prop="loginbt">
            <el-input v-model="ruleForm.loginbt" autocomplete="off" minlength="1" maxlength="20"></el-input>
          </el-form-item> -->
          <el-form-item label="选择" prop="select">
            <el-select v-model="value" placeholder="请选择">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="账号" prop="loginId" v-if="value=='选项1'">
            <el-input v-model="ruleForm.loginId" autocomplete="off"></el-input>
          </el-form-item>
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item v-if="isAdd" label="密码" prop="loginPwd">
                <el-input type="password" v-model="ruleForm.loginPwd" autocomplete="off"></el-input>
               </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item v-if="isAdd" label="确认密码" prop="loginPwd2">
                <el-input type="password" v-model="ruleForm.loginPwd2" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>

          </el-row>
          <!-- 不需要修改密码 -->

          <el-form-item label="姓名" prop="name">
            <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话" prop="phone">
            <el-input v-model="ruleForm.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色" prop="roleId">
            <el-select v-model="ruleForm.roleId" placeholder="请选择角色" clearable>
              <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">{{
              isAdd ? "添加" : "修改"
            }}</el-button>
            <el-button @click="resetForm('ruleForm')">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
// 导入给md5加密方法
// 导入 查看管理员头像根路径 和 管理员头像上传路径
export default {
  // 数据\

  data() {
    // 验证账号
    var validateLoginId = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号信息'))
      } else {
        callback()
      }
    }
    // 验证密码
    var validateLoginPwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    // 验证确认密码
    var validateLoginPwd2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入确认密码'))
      } else if (value !== this.ruleForm.loginPwd) {
        callback(new Error('两次密码输入不一致'))
      } else {
        callback()
      }
    }
    // 验证姓名
    var validateName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入姓名'))
      } else {
        callback()
      }
    }
    // 验证电话
    var validatePhone = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入电话'))
      } else {
        callback()
      }
    }
    // 验证角色
    var validateRoleId = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请选择角色'))
      } else {
        callback()
      }
    }
    return {
      // 瞎捣数组
      newlist: [{ name: '张三', age: 20 }],
      options: [{
        value: '选项1',
        label: '黄金糕'
      }, {
        value: '选项2',
        label: '双皮奶'
      }, {
        value: '选项3',
        label: '蚵仔煎'
      }, {
        value: '选项4',
        label: '龙须面'
      }, {
        value: '选项5',
        label: '北京烤鸭'
      }],
      value: '',
      inputshow: false,
      //   定义角色数组 
      rolelist: [{ id: 1, name: '超级管理员' }, { id: 2, name: '普通管理员' }],
      //   定义表格数组
      tableData: new Array(45).fill({}),
      currentPage: 1, // 当前页码
      pageSize: 10,// 每页的数据条数
      //展示数组
      tableData1: [],
      // 角色编号（用于根据条件查询账号信息）
      roleId: '',
      // 角色列表（用于搜索）
      roleListToSearch: [
        {
          "roleId": 1,
          "roleName": "超级管理员"
        },
        {
          "roleId": 2,
          "roleName": "普通管理员"
        },
        {
          "roleId": 3,
          "roleName": "测试"
        },
        {
          "roleId": 4,
          "roleName": "法外狂徒张三"
        }
      ],
      // 角色列表
      roleList: [
        {
          "roleId": 1,
          "roleName": "超级管理员"
        },
        {
          "roleId": 2,
          "roleName": "普通管理员"
        },
        {
          "roleId": 3,
          "roleName": "测试"
        },
        {
          "roleId": 4,
          "roleName": "法外狂徒张三"
        }
      ],
      // 当前页码
      pageIndex: 1,
      // 是否打开抽屉
      drawer: false,
      // 抽屉是否做添加
      isAdd: true,
      // 表单数据
      ruleForm: {
        // 账号
        loginId: '',
        // 密码
        loginPwd: '',
        // 确认密码
        loginPwd2: '',
        // 姓名
        name: '',
        // 电话
        phone: '',
        // 角色编号
        roleId: '',
        // 头像
        photo: '',
        // 标题
        loginbt: ''
      },
      newruleForm: {

      },
      // 表单的验证数据
      rules: {
        //  { 
        //   validator: function (rule, value, callback) {
        //     if (/^(?:\d*|[a-zA-Z]*|[\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]*)$/.test(value)) {
        //       callback(new Error("不能输入纯数字,字母,符号"));
        //     } else {
        //       callback();
        //     }
        //   }, trigger: 'change'
        // }
        // 验证标题
        loginbt: [{ required: true, pattern: /^(https?:\/\/)?/i, message: '请输入链接', trigger: 'change' },
        ],
        // 验证账号
        loginId: [{ validator: validateLoginId, trigger: 'blur' }],
        // 验证密码
        loginPwd: [{ validator: validateLoginPwd, trigger: 'blur' }],
        // 验证确认密码
        loginPwd2: [{ validator: validateLoginPwd2, trigger: 'blur' }],
        // 验证姓名
        name: [{ validator: validateName, trigger: 'blur' }],
        // 验证电话
        phone: [{ validator: validatePhone, trigger: 'blur' }],
        // 验证角色
        roleId: [{ validator: validateRoleId, trigger: 'change' }]
      }
    }
  },
  created() {

    this.tableData = this.tableData.map((v, i) => {
      return {
        id: i % 2 == 0 ? 1 : 0, roleId: i % 2 == 0 ? this.rolelist[0].id : this.rolelist[1].id, loginId: '测试账户' + i, name: '测试' + i, phone: '1355194978' + Math.floor(Math.random() * 10), role: {
          roleId: i % 2 == 0 ? this.rolelist[0].id : this.rolelist[1].id, roleName: i % 2 == 0 ? this.rolelist[0].name : this.rolelist[1].name
        }
      }
    })
    this.tableData1 = this.tableData
  },
  mounted() {
  },
  watch: {
    value: {
      handler(newval, oldval) {
        if (newval == '选项1') {
          this.ruleForm.loginId = ''
        }
        console.log(newval);
      }
    }
  },
  // 方法
  methods: {
    // 分页方法
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },

    //搜索方法
    searchClick() {

    },
    // 添加按钮点击方法
    addClick() {
      this.isAdd = true
      this.drawer = true
      this.ruleForm = {
        // 账号
        loginId: '',
        // 密码
        loginPwd: '',
        // 确认密码
        loginPwd2: '',
        // 姓名
        name: '',
        // 电话
        phone: '',
        // 角色编号
        roleId: '',
      }
    },
    // 关闭抽屉的方法
    drawerClose(done) {
      // 清空表单数据
      /* this.ruleForm = {
          roleName: "",
      }; */

      this.resetForm('ruleForm')

      // 表单恢复成添加状态
      this.isAdd = true
      // 关闭抽屉
      done()
    },
    // 表单提交方法
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        // 判断表单数据是否验证成功
        if (valid) {
          //判断是否是执行添加
          console.log(this.ruleForm);

        } else {
          return false
        }
      })
    },
    // 表单重置方法
    resetForm(formName) {
      //重置表单
      this.$refs[formName].resetFields()
      //清空表单数据
      this.drawer = false
    },
    // 编辑表格数据
    async handleEdit(index, row) {
      this.isAdd = false;
      //打开抽屉
      this.drawer = true;

    },
    // 删除表格数据
    async handleDelete(index, row) {

    }
  }
}
</script>
<style scoped lang="scss">
:deep(.el-table__body-wrapper::-webkit-scrollbar) {
  width: 8px;
  height: 8px;
}
:deep(.el-table__body-wrapper::-webkit-scrollbar-thumb) {
  border-radius: 10px; /*滚动条的圆角*/
  -webkit-box-shadow: inset 0 0 6px rgba(145, 143, 0143, 0.3);
  background-color: rgb(145, 143, 143); /*滚动条的背景颜色*/
}
:deep(.el-table__body-wrapper::-webkit-scrollbar-track) {
  border-radius: 10px; /*滚动条的背景区域的圆角*/
  -webkit-box-shadow: inset 0 0 6px rgba(238, 238, 238, 0.3);
  background-color: #eeeeee; /*滚动条的背景颜色*/
}

.search {
  padding-left: 10px;
}
// 上传头像样式
.avatar-uploader {
  width: 178px;
  height: 178px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
